<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
    <title>代理商管理系统</title>
    <link rel="stylesheet" type="text/css" href="${ctx}/statics/easyui/themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="${ctx}/statics/easyui/themes/icon.css" />
    <link rel="stylesheet" type="text/css" href="${ctx}/statics/easyui/themes/color.css" />
    <link rel="stylesheet" type="text/css" href="${ctx}/statics/easyui/themes/demo.css" />
    <script src="${ctx}/statics/easyui/jquery.min.js" type="text/javascript"></script>
    <script src="${ctx}/statics/easyui/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="${ctx}/statics/easyui/easyui-lang-zh_CN.js" type="text/javascript"></script>
</head>
<body class="easyui-layout" >
        <div data-options="region:'center',split:false" style="background:#eee;padding:10px 20px 10px 20px">
            <div style="padding-left:10px;margin-bottom:10px">
                <h5 style="margin:0">关键词申请</h5>
                <p>
                    <span style="width:80px;text-align:justify;display:inline-block">
                        当前账户余额:
                    </span>
                    <input id="money" class="easyui-textbox" type="text" name="money" style="width:150px;" readonly/>
                </p>
                <p>
                    <span style="width:80px;text-align:justify;display:inline-block">
                        搜索客户:
                    </span>
                    <input id="customs" name="customs" style="width:150px" />
                    <span>输入后自动搜索</span>
                </p>
            </div>
            <div style="border:1px solid lightgrey;padding-left:10px">
                <form method="post" id="keywordsForm" >
                    <p>
                        <span style="width:80px;text-align:justify;display:inline-block">
                            客户名称:
                        </span>
                        <input id="customName" class="easyui-textbox" type="text" name="customName" style="width:150px;" readonly/>
                        <input type="hidden" name="customId" />
                    </p>
                    <p style="margin-top:20px">
                        <span style="width:80px;text-align:justify;display:inline-block">
                            关键词:
                        </span>
                        <input class="easyui-textbox" type="text" name="keywords" style="width:150px;"
                               data-options="required:true,missingMessage:'关键词不能为空'" />
                    </p>
                    <p style="margin-top:20px">
                        <span style="width:80px;text-align:justify;display:inline-block">
                            服务类别:
                        </span>
                        <input name="productType" id="productType" value="--请选择--" style="width:150px" />
                        <input name="servicePrice" type="hidden" />
                    </p>
                    <p style="margin-top:20px">
                        <span style="width:80px;text-align:justify;display:inline-block">
                            服务年限:
                        </span>
                        <input name="serviceYears" id="serviceYears" value="--请选择--" style="width:150px" />
                    </p>
                    <p style="margin-top:20px">
                        <span style="width:80px;text-align:justify;display:inline-block">
                            价格:
                        </span>
                        <input id="totalPrice" class="easyui-textbox" type="text" style="width:150px;" readonly/>
                        <input type="hidden" name="preRegFrozenMoney" />
                    </p>
                    <p style="margin-top:20px" >
                        <a href="javascript:void(0)" class="easyui-linkbutton c6"
                           onclick="submitForm()" style="width:60px;margin-right:10px;">提交申请</a>
                    </p>
                </form>
            </div>
        </div>

    <script>

        $(
            function ()
            {
                //查询当前账户余额
                $.ajax(
                    {
                        type:"POST",
                        url:"${ctx}/do/comboboxJsonStr",
                        data:{"action":"money"},
                        dataType:"json",
                        success:function( json )
                        {
                            $("#money").textbox('setValue' , json.money );
                        }
                    });

                //查询服务类别
                $("#productType").combobox(
                    {
                        url:'${ctx}/do/comboboxJsonStr',
                        queryParams:{"action":"productType"},
                        valueField:'configTypeValue',
                        textField:'configTypeName',
                        editable: false,
                        onSelect:function( rec )
                        {
                            //设置隐藏域的value为当前选择的服务价格
                            var price = parseFloat( rec.configValue );
                            //设置预冻结资金
                            $("[name=preRegFrozenMoney]").val( price );
                            //alert( price );
                            $("[name=servicePrice]").val( price );
                            calculatePrice();
                        }
                    });

                //查询服务年限
                $("#serviceYears").combobox(
                    {
                        url:'${ctx}/do/comboboxJsonStr',
                        queryParams:{"action":"serviceYears"},
                        valueField:'years',
                        textField:'years',
                        editable: false,
                        onSelect:function( rec )
                        {
                            calculatePrice();
                        }
                    });

                //查询当前代理商所有可用客户
                $("#customs").combobox(
                    {
                        url:'${ctx}/do/comboboxJsonStr',
                        queryParams:{"action":"customs"},
                        valueField:'id',
                        textField:'customName',
                        editable: true,
                        onSelect:function( rec )
                        {

                            //设置客户名称textbox
                            $("#customName").textbox('setValue',rec.customName);
                            //设置隐藏域值为客户id
                            $("[name=customId]").val( rec.id )
                        }
                    });
            }
        );

        //根据服务类型和服务年限设置价格
        function calculatePrice()
        {
            //获取单价
            var price = $("[name=servicePrice]").val();
            //alert("单价: " + price );
            //alert( typeof price );
            var years = $("#serviceYears").combobox('getValue');
            //alert( typeof years );
            if( price != "--请选择--" && years != "--请选择--" )
            {
                var total = parseFloat( price ) * parseInt( years );
                $("#totalPrice").textbox('setValue' , total );
            }

        }

        //点击保存按钮提交表单
        function submitForm()
        {
            //设置表单请求
            $("#keywordsForm").form(
                {
                    url:"${ctx}/do/keywordsApplication",
                    onSubmit: function(param){
                        param.detailType = 2;
                    },
                    //提交成功后处理返回值
                    success:function(data)
                    {
                        //注意需要将返回值转换为json
                        var json = JSON.parse(data);
                        //alert(json.message);
                        if( json.message == 1 )
                        {
                            $.messager.alert('申请操作','申请成功--请等待管理员审核','info',
                                function(result)
                                {
                                    window.location.href = "${ctx}/do/toKeywordsApplicationManagePage";
                                    //申请成功跳转到关键词管理页面

                                });
                        }
                        else
                        {
                            $.messager.alert( '申请操作','申请失败--该关键词已被申请,请更换后重试','info' );
                        }
                    }
                });
            //提交表单
            $("#keywordsForm").submit();
        }
    </script>
</body>
</html>
